package outputviewer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.*;

public class Buttons extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 90332140929163986L;
	private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
	ArrayList<JTextArea> textBoxes = new ArrayList<JTextArea>();
	private int lastUsed = 1;
	private Canvas myCanvas;
    public Buttons(Canvas c) {
    	myCanvas = c;

        b1 = new JButton("<<");
        b1.setMnemonic(KeyEvent.VK_D);
        b1.setActionCommand("rr");
        b1.addActionListener(this);
        
        b2 = new JButton("<");
        b2.setMnemonic(KeyEvent.VK_D);
        b2.setActionCommand("r");
        b2.addActionListener(this);

        b3 = new JButton("Step -");
        b3.setMnemonic(KeyEvent.VK_D);
        b3.setActionCommand("sr");
        b3.addActionListener(this);

        b4 = new JButton("Pause");
        b4.setMnemonic(KeyEvent.VK_M);
        b4.setActionCommand("p");
        b4.addActionListener(this);
        
        b5 = new JButton("Step +");
        b5.setMnemonic(KeyEvent.VK_D);
        b5.setActionCommand("sf");
        b5.addActionListener(this);

        b6 = new JButton(">");
        b6.setMnemonic(KeyEvent.VK_E);
        b6.setActionCommand("f");
        b6.addActionListener(this);

        b7 = new JButton(">>");
        b7.setMnemonic(KeyEvent.VK_E);
        b7.setActionCommand("ff");
        b7.addActionListener(this);
        
        add(b1);
        add(b2);
        add(b3);
        add(b4);
        add(b5);
        add(b6);
        add(b7);
        
        if (myCanvas.myRelativeWidths.length > 1)
        {
	        for (int i = 0; i < myCanvas.myRelativeWidths.length; i++)
	        {
	        	textBoxes.add(new JTextArea(Integer.toString(myCanvas.myRelativeWidths[i]), 1, 3));
	        	add(textBoxes.get(i));
	        }
	        b8 = new JButton("Set sizes");
	        b8.setMnemonic(KeyEvent.VK_E);
	        b8.setActionCommand("size");
	        b8.addActionListener(this);
	        //Add Components to this container, using the default FlowLayout.
	        add(b8);
        }

        b9 = new JButton("Screenshot");
        b9.setMnemonic(KeyEvent.VK_E);
        b9.setActionCommand("screen");
        b9.addActionListener(this);
        add(b9);
        
    }

    public void actionPerformed(ActionEvent e) {
        if ("p".equals(e.getActionCommand())) {
            myCanvas.setFrameRate(0);
        } else if ("f".equals(e.getActionCommand())) {
            myCanvas.setFrameRate(1);
        } else if ("r".equals(e.getActionCommand())) {
            myCanvas.setFrameRate(-1);
        } else if ("sf".equals(e.getActionCommand())) {
            myCanvas.setFrameRate(0);
            myCanvas.addAmount(1);
        } else if ("ff".equals(e.getActionCommand())) {
        	if (lastUsed < 0)
        		lastUsed = 1;
        	lastUsed *= 2;
            myCanvas.setFrameRate(lastUsed);
        } else if ("rr".equals(e.getActionCommand())) {
        	if (lastUsed > 0)
        		lastUsed = -1;
        	lastUsed *= 2;
            myCanvas.setFrameRate(lastUsed);
        } else if ("sr".equals(e.getActionCommand())) {
            myCanvas.setFrameRate(0);
            myCanvas.addAmount(-1);
        } else if ("size".equals(e.getActionCommand())) {
            boolean canSet = true;
            for (int i = 0; i < textBoxes.size(); i++)
            {
            	try
            	{
            		Integer.parseInt(textBoxes.get(i).getText());
            	}
            	catch (NumberFormatException error)
            	{
            		canSet = false;
            	}
            }
            if (canSet)
            {
            	for (int i = 0; i < myCanvas.myRelativeWidths.length; i++)
            	{
            		myCanvas.myRelativeWidths[i] = Math.max(Integer.parseInt(textBoxes.get(i).getText()), 1);
            	}
            }
            for (int i = 0; i < textBoxes.size(); i++)
            	textBoxes.get(i).setText(Integer.toString(myCanvas.myRelativeWidths[i]));
        } else if ("screen".equals(e.getActionCommand())) {
        	myCanvas.takeScreenshot();
        }
    }
}
